package org.codeart.map.remote;

import org.codeart.common.dto.DicDistrict;
import org.codeart.common.util.UrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
public class MapDicDistrictClient {

    // https://restapi.amap.com/v3/config/district?keywords=%E5%8D%97%E4%BA%AC&subdistrict=2
    @Value("${system.map.district-api}")
    private String districtUrl;

    @Value("${system.map.key}")
    private String userKey;
    
    @Autowired
    private RestTemplate restTemplate;

    public String dicDistrict(String keywords) {
        Map<String, Object> conditions = new HashMap<String, Object>() {{
            put("keywords", keywords);
            put("subdistrict", "3");
            put("key", userKey);
        }};
        String url = UrlUtil.createQueryParameters(districtUrl, conditions);
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
        return response.getBody();
    }
}
